package com.dev.itb.service;

import org.springframework.stereotype.Service;

import com.dev.itb.domain.Person;

import dev.meow.core.PersonData;
import dev.meow.core.SMCCore;

@Service
public class SMCService {

	private SMCCore smcCore;

	public SMCService() {
		System.out.println("################ install " + getClass().getName()+ "########################");
		smcCore = new SMCCore();
	}

	public synchronized PersonData getDATA() {
		PersonData data = smcCore.getTH_IC_DATA();
		System.out.println(data);
		return data;
	}

	public synchronized Person  getPerson() {
		PersonData data = smcCore.getTH_IC_DATA();
		if(data == null) return null;
		
		Person person = new Person();
		String tName = data.getPersonInfo().substring(0, 100);
		//TH
		String[] tNames = tName.trim().split("#");
		person.setPreNameThai(tNames[0]);
		person.setFirstNameThai(tNames[1]);
		person.setLastNameThai(tNames[3]);
		//ENG
		String eName = data.getPersonInfo().substring(100, 200);
		String[] eNames = eName.trim().split("#");
		person.setPreNameEng(eNames[0]);
		person.setFirstNameEng(eNames[1]);
		person.setLastNameEng(eNames[3]);
		//HBD
		String birthday = data.getPersonInfo().substring(200, 208);
		person.setBirthDateStr(birthday);
		String sex = data.getPersonInfo().substring(208, 209);
		person.setSex(sex);
		
		//ADDR
		person.setAddressStr( data.getAddr());
		
		//EXP
		person.setExpiredDateStr( data.getExpire());
		
		//picture
		person.setImageData(data.getImage());
		
		//ID
		person.setCitizenId( data.getpId());
		
		return person;
	}

	public synchronized byte[] getPicture(){
		return smcCore.getPhoto();
	}
	public synchronized boolean getStatus() {
		return smcCore.checkStatus();
	}
}
